﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PegasCompany.Data;
using PegasCompany.Helper;
using PegasCompany.Tools;
using Resources;

namespace PegasCompany
{
    public partial class News : System.Web.UI.Page
    {
        public int CurrentYear { get; set; }
        public int CurrentMonth { get; set; }
        public int CurrentPage { get; set; }
        public int CurrentSiteContentId { get; set; }

        private void ReadQueryString()
        {
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                int iSiteContentId;
                if (int.TryParse(Request.QueryString["id"], out iSiteContentId))
                {
                    CurrentSiteContentId = iSiteContentId;
                }
            }
            else
            {
                if (String.IsNullOrEmpty(Request.QueryString["year"]))
                {
                    GetLastYear();
                    Response.Redirect(String.Format("/News.aspx?year={0}", CurrentYear));
                }
                else
                {
                    int iYear;
                    if (!int.TryParse(Request.QueryString["year"], out iYear))
                    {
                        GetLastYear();
                        Response.Redirect(String.Format("/News.aspx?year={0}", CurrentYear));
                    }
                    else
                    {
                        CurrentYear = iYear;
                    }
                }

                if (String.IsNullOrEmpty(Request.QueryString["month"]))
                {
                    GetLastMonth();
                    Response.Redirect(String.Format("/News.aspx?year={0}&month={1}", CurrentYear, CurrentMonth));
                }
                else
                {
                    int iMonth;
                    if (!int.TryParse(Request.QueryString["month"], out iMonth))
                    {
                        GetLastMonth();
                        Response.Redirect(String.Format("/News.aspx?year={0}&month={1}", CurrentYear, CurrentMonth));
                    }
                    else
                    {
                        if (iMonth > 12)
                        {
                            Response.Redirect(String.Format("/News.aspx?year={0}", CurrentYear));
                        }
                        else
                        {
                            CurrentMonth = iMonth;
                        }
                    }
                }

                if (!String.IsNullOrEmpty(Request.QueryString["page"]))
                {
                    int iCurrentPage;
                    if (int.TryParse(Request.QueryString["page"], out iCurrentPage))
                    {
                        CurrentPage = iCurrentPage;
                    }
                    else
                    {
                        Response.Redirect(String.Format("/News.aspx?year={0}&month={1}&page=1", CurrentYear,
                            CurrentMonth));
                    }
                }
                else
                {
                    Response.Redirect(String.Format("/News.aspx?year={0}&month={1}&page=1", CurrentYear, CurrentMonth));
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Title = String.Format("{0} - {1}", UserSite.resCompanyName, UserSite.resNews);

            if (!Page.IsPostBack)
            {
                ReadQueryString();

                if (CurrentYear != 0 && CurrentMonth != 0)
                {
                    FillMessages((int) EnumHelper.ListContentType.News, CurrentYear, CurrentMonth);
                }

                if (CurrentSiteContentId != 0)
                {
                    SiteContent sc = SiteContent.Get(CurrentSiteContentId);
                    if (sc != null)
                    {
                        mvCompanyMessages.ActiveViewIndex = 1;
                        lContent.Text = sc.Content;
                        lContentTitle.Text = sc.Title;
                        lTitle.Text = sc.Title;
                        lDate.Text = sc.CreateDate.ToString();
                        lContentDescription.Text = sc.CreateDate.ToString();
                        //rptFiles.DataSource = sc.FileAttachements;
                        //rptFiles.DataBind();

                        Title = String.Format("{0} - {1}", UserSite.resCompanyName, sc.Title);
                    }
                }

                //CurrentYear = FillYears();
                //if (!Request.QueryString.HasKeys())
                //{
                //    FillMessages(CurrentYear);
                //}
                //else if (!String.IsNullOrEmpty(Request.QueryString["year"]))
                //{
                //    string sYear = Request.QueryString["year"];
                //    int iYear;

                //    if (int.TryParse(sYear, out iYear))
                //    {
                //        CurrentYear = iYear;
                //        FillMessages(iYear);
                //    }
                //}
                //else if (!String.IsNullOrEmpty(Request.QueryString["Id"]))
                //{
                //    string sSiteContentId = Request.QueryString["Id"];
                //    int iSiteContentId;

                //    if (int.TryParse(sSiteContentId, out iSiteContentId))
                //    {
                //        SiteContent sc = SiteContent.Get(iSiteContentId);
                //        if (sc != null)
                //        {
                //            mvCompanyMessages.ActiveViewIndex = 1;
                //            lContent.Text = sc.Content;
                //            lContentTitle.Text = sc.Title;
                //            lTitle.Text = sc.Title;
                //            lDate.Text = sc.CreateDate.ToString();
                //            lContentDescription.Text = sc.CreateDate.ToString();
                //            //rptFiles.DataSource = sc.FileAttachements;
                //            //rptFiles.DataBind();

                //            Title = String.Format("{0} - {1}", UserSite.resCompanyName, sc.Title);
                //        }
                //    }
                //}
            }
        }

        private void CreatePager(int year, int month, int fullPages)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < fullPages; i++)
            {
                sb.AppendFormat(CurrentPage == i + 1 ? "<a class=\"active\">{2}</a>" : "<a href=\"News.aspx?year={0}&month={1}&page={2}\">{2}</a>", year, month, i + 1);
            }
            lPager.Text = sb.ToString();
        }

        private void GetLastYear()
        {
            List<int> lstYear = SiteContent.ListAllContentYear((int)EnumHelper.ListContentType.News);
            if (lstYear == null)
            {
                
            }
            else
            {
                lstYear = new List<int>(lstYear.OrderByDescending(i => i));
                CurrentYear = lstYear.First();
            }
        }

        protected void GetLastMonth()
        {
            List<int> lstMonth = SiteContent.ListAllContentYearMonth((int)EnumHelper.ListContentType.News, CurrentYear);
            if (lstMonth == null)
            {
                Response.Redirect(String.Format("/News.aspx?year={0}", CurrentYear));
            }
            else
            {
                CurrentMonth = lstMonth.First();
            }
        }

        private void FillMessages(int contentType, int year, int month)
        {
            mvCompanyMessages.ActiveViewIndex = 0;

            lContentTitle.Text = String.Format("{0} ({1})", UserSite.resNews, year);

            int newsCount = PageData.GetRecordCount(contentType, year, month);
            int pageSize = ConfigReader.PageSize;

            int iFullPages = newsCount % pageSize == 0
                ? newsCount / pageSize
                : (newsCount / pageSize) + 1;

            if (CurrentPage > iFullPages)
            {
                Response.Redirect(String.Format("/News.aspx?year={0}&month={1}&page=1", CurrentYear, CurrentMonth));
            }

            List<SiteContent> newsList = SiteContent.GetPage(contentType, CurrentPage, pageSize, year, month);
            rptList.DataSource = newsList;
            rptList.DataBind();

            CreatePager(year, month, iFullPages);

            //List<SiteContent> scList = SiteContent.ListAllContent((int)EnumHelper.ListContentType.News, true, iYear);
            //rptList.DataSource = scList.OrderByDescending(t => t.CreateDate);
            //rptList.DataBind();
        }

        protected string GetYearStatus(object oYear)
        {
            if (oYear is int)
            {
                int iYear = Convert.ToInt32(oYear);
                return iYear == CurrentYear ? "active" : string.Empty;
            }
            return string.Empty;
        }
    }
}